/*
 * @Author: 
 * @Date: 2024-11-28 16:35:17
 * @LastEditor: zhuxiaoyi
 * @LastEditTime: 2025-01-21 18:02:55
 * @Description: 用户相关 DTO 定义
 */
import { ApiProperty } from '@midwayjs/swagger';

export class CreateUserDto {
  @ApiProperty({
    example: 'john_doe',
    description: '用户名，必须唯一',
  })
  username: string;

  @ApiProperty({
    example: 'john@example.com',
    description: '用户邮箱，必须唯一',
  })
  email: string;

  @ApiProperty({
    example: 'password123',
    description: '用户密码',
  })
  password: string;

  @ApiProperty({
    example: 'roleId123',
    description: '角色 ID',
    required: false,
  })
  roleId?: string;

  @ApiProperty({
    example: 'subsystemId456',
    description: '子系统 ID',
    required: false,
  })
  subsystemId?: string;

  /**
   * 校验数据的静态方法
   */
  static validate(data: Partial<CreateUserDto>) {
    const errors: string[] = [];

    // Validate username
    if (!data.username || typeof data.username !== 'string') {
      errors.push('用户名必须是字符串类型');
    }

    // Validate email format
    if (!data.email || !/^\S+@\S+\.\S+$/.test(data.email)) {
      errors.push('邮箱格式无效');
    }

    // Validate password length
    if (!data.password || typeof data.password !== 'string' || data.password.length < 6) {
      errors.push('密码长度至少为 6 个字符');
    }

    // Validate roleId if provided
    if (data.roleId && typeof data.roleId !== 'string') {
      errors.push('roleId 必须是字符串');
    }

    // Validate subsystemId if provided
    if (data.subsystemId && typeof data.subsystemId !== 'string') {
      errors.push('subsystemId 必须是字符串');
    }

    return errors;
  }
}


export class UpdateUserDto {
  @ApiProperty({
    example: 'new_email@example.com',
    description: '新的邮箱地址',
    required: false,
  })
  email?: string;

  @ApiProperty({
    example: 'new_password123',
    description: '新的密码',
    required: false,
  })
  password?: string;

  @ApiProperty({
    example: true,
    description: '是否激活用户',
    required: false,
  })
  isActive?: boolean;

  @ApiProperty({
    example: '12345',
    description: '角色 ID',
    required: false,
  })
  roleId?: string;

  @ApiProperty({
    example: '67890',
    description: '子系统 ID',
    required: false,
  })
  subsystemId?: string;

  /**
   * 校验数据的静态方法
   */
  static validate(data: Partial<UpdateUserDto>) {
    const errors: string[] = [];

    // Validate email format
    if (data.email && !/^\S+@\S+\.\S+$/.test(data.email)) {
      errors.push('邮箱格式无效');
    }

    // Validate password length (if provided)
    if (data.password && (typeof data.password !== 'string' || data.password.length < 6)) {
      errors.push('密码长度至少为 6 个字符');
    }

    // Validate isActive type (if provided)
    if (data.isActive !== undefined && typeof data.isActive !== 'boolean') {
      errors.push('isActive 必须是布尔值');
    }

    // Validate roleId (if provided)
    if (data.roleId && typeof data.roleId !== 'string') {
      errors.push('roleId 必须是字符串');
    }

    // Validate subsystemId (if provided)
    if (data.subsystemId && typeof data.subsystemId !== 'string') {
      errors.push('subsystemId 必须是字符串');
    }

    return errors;
  }
}

export class LoginDto {
  @ApiProperty({
    example: 'john_doe',
    description: '用户名',
  })
  username: string;

  @ApiProperty({
    example: 'password123',
    description: '用户密码',
  })
  password: string;

  /**
   * 校验数据的静态方法
   */
  static validate(data: Partial<LoginDto>) {
    const errors: string[] = [];

    if (!data.username || typeof data.username !== 'string') {
      errors.push('用户名必须是字符串类型');
    }

    if (!data.password || typeof data.password !== 'string' || data.password.length < 6) {
      errors.push('密码长度至少为 6 个字符');
    }

    return errors;
  }
}
